package com.five.dao.impl.backstage;

import com.five.dao.backstage.FacilityDao;
import com.five.entity.backstage.Facility;
import com.five.util.DBUtil;
import com.five.util.Page;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Author: 常鹏宇
 * @Description:
 * @Date: 2020/11/12 11:42
 */
@Repository
public class FacilityDaoImpl implements FacilityDao {
    @Override
    public List<Map> selectFacility(Page pageInfo, Facility facility) {
        String sql = "select a.id,a.name,a.status,count,b.id bid,b.name typeid,pmoney,gys \n" +
                    "from t_facility a \n" +
                    "left join t_ptype b \n" +
                    "on a.typeid = b.id\n" +
                    "where 1=1";
        if (facility.getSname()!=null && facility.getSname()!=""){
            sql+=" and a.name like '%"+facility.getSname()+"%'";
        }
        if (facility.getStypeid()!=null && facility.getStypeid()!=""){
            sql+=" and b.name like '%"+facility.getStypeid()+"%'";
        }
        sql+=" group by id limit ?,?";
        int pagesize = pageInfo.getLimit();
        int star = (pageInfo.getPage()-1)*pagesize;
        List<Map> maps = DBUtil.queryMap(sql,star,pagesize);
        return maps;
    }

    @Override
    public int count(Facility facility) {
        String sql = "select count(*) a\n" +
                    "from t_facility a \n" +
                    "left join t_ptype b \n" +
                    "on a.typeid = b.id\n" +
                    "where 1=1";
        if (facility.getSname()!=null && facility.getSname()!=""){
            sql+=" and a.name like '%"+facility.getSname()+"%'";
        }
        if (facility.getStypeid()!=null && facility.getStypeid()!=""){
            sql+=" and b.name like '%"+facility.getStypeid()+"%'";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        String a = maps.get(0).get("a") + "";
        int i = Integer.parseInt(a);
        return i;
    }

    @Override
    public List<Map> selectTypeid() {
        String sql = "select * from t_ptype where status=0 ";
        return DBUtil.queryMap(sql);
    }

    @Override
    public int addAndUpdFAC(Facility facility) {
        if (facility.getId()==null){
            String sql = "insert into t_facility(id,name,count,typeid,pmoney,gys)  values(null,?,?,?,?,?)";
            return DBUtil.manage(sql,facility.getName(),facility.getCount(),
                                    facility.getTypeid(),facility.getPmoney(),facility.getGys());
        }else {
            String sql = "update t_facility set name=?,count=?,typeid=?,pmoney=?,gys=? where id =?";
            return DBUtil.manage(sql,facility.getName(),facility.getCount(),
                                    facility.getTypeid(),facility.getPmoney(),facility.getGys(),facility.getId());
        }
    }

    @Override
    public int delFacility(Facility facility) {
        String sql = "delete from t_facility where id = ?";
        return DBUtil.manage(sql,facility.getId());
    }
    @Override
    public int bad(Integer id) {
        String sql="update t_facility set status=1 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }

    @Override
    public int good(Integer id) {
        String sql="update t_facility set status=0 where id=?";
        int i = DBUtil.manage(sql, id);
        return i;
    }



    /*@Override
    public int addFacility(Facility facility) {
        String sql = "insert into t_facility(name,count,typeid,pmoney,gys) values(?,?,?,?,?)";
        return DBUtil.manage(sql,facility.getName(),facility.getCount(),facility.getTypeid(),facility.getPmoney(),facility.getGys());
    }


    @Override
    public List<Map> selectFacility(String page,String limit,Facility facility) {
        int pp = Integer.parseInt(page);
        int ll = Integer.parseInt(limit);
        String sql = "select * from t_facility where 1=1 ";
        if (facility.getId()!=null){
            sql+=" and id="+facility.getId()+" ";
        }
        if (facility.getName()!=null){
            sql+=" and name like '%"+facility.getName()+"%' ";
        }

        sql+=" limit ?,?";
        List<Map> maps = DBUtil.queryMap(sql,(pp-1)*ll,ll);
        return maps;
    }
    @Override
    public List<Map> selectFacById(String id) {
        String sql = "select * from t_facility where id=? ";
        List<Map> maps = DBUtil.queryMap(sql,id);
        return maps;
    }

    @Override
    public int getcount(Facility facility) {
        String sql="select count(*) a from t_facility where 1=1 ";
        if (facility.getId()!=null){
            sql+=" and id="+facility.getId()+" ";
        }
        if (facility.getName()!=null){
            sql+=" and name like '%"+facility.getName()+"%' ";
        }
        List<Map> maps = DBUtil.queryMap(sql);
        String s = maps.get(0).get("a") + "";
        int i = Integer.parseInt(s);
        return i;
    }



    @Override
    public List<Map> selectFacByTypeId(String typeId) {
        String sql = "select * from t_facility where typeid=? ";
        List<Map> maps = DBUtil.queryMap(sql,typeId);
        return maps;
    }

    @Override
    public int delFacility(String id) {
        String sql = "delete from t_facility where id = ?";
        return DBUtil.manage(sql,id);
    }

    @Override
    public int update(Facility facility) {
        String sql = "update t_facility set name = ?,count=?,typeid=?,pmoney=?,gys=? where id =?";
        return DBUtil.manage(sql, facility.getName(), facility.getCount(), facility.getTypeid(), facility.getPmoney(), facility.getGys(), facility.getId());
    }

*/
}
